<template>
  <div class="details">
    <el-card class="mycard">
      <el-button v-if="activeName !=='first'" type="text" class="mybtn" @click="toPrint"><i class="el-icon-printer"></i></el-button>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录账户设置" name="first">
          <el-form class="myform" :model="form" label-width="100px">
            <el-form-item label="姓名">
              <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="form.password" type="password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="submit" type="primary">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <userInfo :outBaseInfo="getBaseInfo"></userInfo>
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">
          <jobInfo></jobInfo>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getBaseInfoApi, saveBaseInfoApi } from "@/api/employees";

export default {
  data () {
    return {
      activeName: 'first',
      // 表单数据源
      form: {
        username: '',
        password: ''
      },
      // 接收 id
      id: this.$route.query.id
    }
  },
  methods: {
    toPrint(){
// 跳转到打印页面
this.$router.push(`/employees/print?id=${this.id}&active=${this.activeName}`)
    },
    async submit() {
      try {
         // 提交数据到服务器
      await saveBaseInfoApi(this.form);
      // 提示成功
      this.$message({
        message: "更新数据成功",
        type: "success",
      }); 

      this.getBaseInfo()

      } catch (error) {
          console.log('更新出错啦');
      }
    },
    async getBaseInfo() {
      // 提交数据到服务器
      const res= await getBaseInfoApi(this.id);
    //   console.log(res);
    this.form = res.data
    },
  },
    created () {
    this.getBaseInfo()
  },
    components: {
    userInfo: () => import('../components/user-info.vue'),
    jobInfo: () => import('../components/job-info.vue')
  }
 
};
</script>

<style lang="scss" scoped>
.details {
  padding: 20px;
  .myform {
    width: 500px;
    margin: 50px 0px 50px 200px;
  }
  .mycard{
    position: relative;
  }
  .mybtn{
    position: absolute;
    right: 20px;
    font-size: 20px;
    z-index: 999999;
  }
}
</style>